#ifndef __LIST_H__
#define __LIST_H__

/**
 * Single linked list in linux style.
 */

/*
 * There is one significant limitation - list_head member
 * must always be first in container. This simplifies list
 * use a bit howeve.
 */

struct list_head {
	struct list_head *next;
};

static inline void init_list_head(struct list_head *list)
{
	list->next = NULL;
}

/**
 * list_empty - tests whether a list is empty
 * @head: the list to test.
 */
static inline int list_empty(const struct list_head *head)
{
	return head->next == NULL;
}

/**
 * list_add_head - add a new entry
 * @new: new entry to be added
 * @head: list head to add it after
 *
 * Insert a new entry after the specified head.
 */
static inline void list_add_head(struct list_head *new,
                                 struct list_head *head)
{
  new->next = head->next;
	head->next = new;
}

/**
 * list_add_tail - add a new entry
 * @new: new entry to be added
 * @head: list head to add it before
 *
 * Insert a new entry before the specified head.
 */
static inline void list_add_tail(void *new,
                                 struct list_head *last)
{
  ((struct list_head *)new)->next = NULL;
  last->next = new;
}

/**
 * list_del_first - deletes first entry from list.
 * @head: the head to delete from the list.
 */
static inline void list_del_first(struct list_head *head)
{
  head->next = head->next->next;
}

/**
 * list_first_entry - get the first element from a list
 * @ptr:	the list head to take the element from.
 * @type:	the type of the struct this is embedded in.
 *
 * Note, that list is expected to be not empty.
 */
#define list_first_entry(ptr, type)             \
	((type *)ptr->next)

/**
 * list_for_each	-	iterate over a list
 * @pos:	the &struct list_head to use as a loop cursor.
 * @head:	the head for your list.
 */

#define list_for_each(pos, head) \
	for (pos = (head)->next; pos != NULL; pos = pos->next)

/**
 * list_for_each_entry	-	iterate over list of given type
 * @pos:	the type * to use as a loop cursor.
 * @head:	the head for your list.
 */
#define list_for_each_entry(pos, head)                       \
	for (pos = (typeof(pos))head->next;                        \
       pos != NULL;                                          \
       pos = (typeof(pos))((struct list head *)pos)->next)

/**
 * list_for_each_entry_safe	-	iterate over list of given type safely
 * @pos:	the type * to use as a loop cursor.
 * @head:	the head for your list.
 * @temp: temporary list head
 */
#define list_for_each_entry_safe(pos, head, temp)             \
	for (pos = (typeof(pos))head->next,                         \
         temp = (head->next) ? (head->next->next) : NULL;     \
       pos != NULL;                                           \
       pos = (typeof(pos))temp,                               \
         temp = temp ? (temp->next) : NULL)



#endif
